package com.jt.blog.controller.AdminManage;

import com.alibaba.druid.support.json.JSONUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.jt.blog.common.model.Page;
import com.jt.blog.common.model.Response;
import com.jt.blog.model.Blog;
import com.jt.blog.service.BlogReadService;
import com.jt.blog.service.BlogWriteService;
import com.jt.blog.vo.BlogQuery;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : 戴瑞
 * @Description :
 * @create : 2018-03-24 15:14
 **/

@Controller
@RequestMapping("/back/admin")
public class AdminBlogController {


    @Autowired
    private BlogReadService blogReadService;

    @Autowired
    private BlogWriteService blogWriteService;

    @RequestMapping("/blogList")
    public String articleShow() {
        return "back/blog/list";
    }

    @RequestMapping("/data/blogList")
    @ResponseBody
    public Object getArticleList(Integer offset, Integer limit, BlogQuery query) {
        Map<String, Object> map = new HashMap<>();
        // 只查询博主公开的博客
        Page<Blog> page = new Page<>();
        page.setPageSize(limit);
        // 求当前页
        page.setPageNo((offset + limit) / limit);
        Response<Page<Blog>> response = blogReadService.getAdminBlogList(page, query);
        if (response.isSuccess()) {
            map.put("total", page.getTotal());
            map.put("rows", page.getRows());
        } else {
            return response;
        }
        return map;
    }

    @RequestMapping("/update/blogstatus")
    @ResponseBody
    public Response<String> updateBlogStatus(HttpServletRequest request) throws IOException {
        Response<String> response = new Response<>();
        Object bloglist = request.getParameter("blogs");
        ObjectMapper mapper = new ObjectMapper();

        Blog[] blogs = mapper.readValue(bloglist.toString(), Blog[].class);
        if (ArrayUtils.isEmpty(blogs)) {
            response.setError("更新失败，请重试！");
        }
        blogWriteService.updateStatus(blogs);
        response.setResult("隐藏此博文成功");

        return response;
    }

    @RequestMapping("/delete/blog")
    @ResponseBody
    public Response<String> deleteBlog(@RequestParam("ids") Long[] ids) {
        Response<String> response = new Response<>();
        if (ArrayUtils.isEmpty(ids)) {
            response.setError("错误！ID为空");
        } else {
            blogWriteService.delete(ids);
            response.setResult("删除成功");
        }
        return response;
    }
}
